package com.push.notifications;
import com.push.notifications.R;
import com.push.notifications.historyAdapterListNews.View_Holder_news;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.TextView;

 public class historyNewsCategory extends Activity  {

    ListView listView2;
    historyAdapterListNews adapter;
    static boolean isDie = true;
    public static Handler han_historyNewsCategory;
    
    public static categoryItem curItem = null;
    
    public void onCreate(Bundle icicle) {

        super.onCreate(icicle);
        setContentView(R.layout.history_category_listview);
        listView2 = (ListView) findViewById(R.id.history_news);
        
        int i = newsTabManager.mTabHost.getCurrentTab();
		historyNewsCategory.curItem = newsTabManager.listSubNews.get(i);
        if(curItem != null)
        {        	
        	int nCatrgoryId = curItem.getCategory();
        	int nParentId = curItem.getParentId();
        	String sCommand = curItem.getCommand();
            adapter = new historyAdapterListNews(this,getModel(nCatrgoryId,nParentId,sCommand));
        }
        
        listView2.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        
        isDie = false;
        han_historyNewsCategory = new Handler() {

            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                switch(msg.what) {

                case 0:
                	finish();
                    break;

                }
            }

        };
        
    }
    private List<historyItem> getModel(int Categoryid, int parentId, String command) {    	
    	  	
    	if(command.equalsIgnoreCase("title"))
    	{
    		return databaseWrapper.getNewsByCategory(this,Categoryid);
    	}
    	else
    	{
    		return databaseWrapper.getMessageById(this,Categoryid);
    	}
    	    	
    }
    
    public  OnClickListener onShowMessage = new OnClickListener() {
		public void onClick(View MesageClick) {
				// lay vi tri
				View_Holder_news viewHolder_here = null;
				viewHolder_here = (View_Holder_news) MesageClick.getTag();
				int positon_here = viewHolder_here.post;
				historyItem   Itemselected =adapter.getListMessage().get(positon_here);	
				if(Itemselected != null)
				{
					int pos = newsTabManager.mTabHost.getCurrentTab();
					String sName = newsTabManager.mTabHost.getCurrentTabTag();
					
					//replace NewsActivity by MessageActivity
					MessageActivity.messageCataloge= sName;
					String content = "";
					if(Itemselected.getCommand().equalsIgnoreCase("title"))
					{
						MessageActivity.title = Itemselected.getName();
						MessageActivity.url = Definitions.LINK_CONTENT + Itemselected.getContentId();
						
					}
					else
					{
						MessageActivity.url = "";
						content = "<html> <body> <p>" + Itemselected.getcontent() + " </p> </body> </html>";
			
					}
					MessageActivity.messageConent = content;
		    	   	Intent intent_message = new Intent(historyNewsCategory.this , MessageActivity.class);
		    	   	startActivity(intent_message);
				}
		    }
		};
		protected void onDestroy() {
			isDie = true;
			super.onDestroy();
		};
		@Override
		public void finish() {
			isDie = true;
			// TODO Auto-generated method stub
			newsTabManager.han_newTabmanager.sendEmptyMessage(0);
			super.finish();
		}
		@Override
		protected void onStart() {
			Log.e("historyNewsCategory", "onStart: ------onStart------------ -");
			super.onStart();
		}
		@Override
		protected void onStop() {
			if(GCMIntentService.AppInterrupt)
			{
				NotificationActivity.han_notification.sendEmptyMessage(0);
				finish();
			}
			Log.e("historyNewsCategory", "onStop: ------onStop------------ "+GCMIntentService.AppInterrupt);
			super.onStop();
		}
		  @Override
		  	public void onWindowFocusChanged(boolean hasFocus) {
			  if (hasFocus)
			  {
				  GCMIntentService.AppInterrupt =false;
			  }
			// TODO Auto-generated method stub
			  Log.e("historyNewsCategory", ": ------onWindowFocusChanged------------ onWindowFocusChanged-"+ GCMIntentService.AppInterrupt);
			super.onWindowFocusChanged(hasFocus);
		  	}
			@Override
			protected void 	onUserLeaveHint(){
				GCMIntentService.AppInterrupt =true;
				Log.e("historyNewsCategory", "onUserLeaveHint: ------onUserLeaveHint------------ "+ GCMIntentService.AppInterrupt);
				super.onUserLeaveHint();
			}

} 